//
//  Message.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/7.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "Message.h"

@implementation ReceiptMessage

/**
 *  将ReceiptMessage转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonReceiptMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":%@,",@"message",[self.message jsonMessage]];
    [msg appendFormat:@"\"%@\":%ld",@"receiptAction",self.receiptAction];
    [msg appendFormat:@"}"];
    return msg;
}

/**
 *  将字典转ReceiptMessage
 *
 *  @param dic 回执消息字典 -- 这个字典为回执消息JSON字符串转化而来
 *
 *  @return 回执消息
 */
+(ReceiptMessage *)receiptMessageWithDic:(NSDictionary *)dic
{
    return [[ReceiptMessage alloc]initWithDic:dic];
}

/**
 *  重写init方法，用于回执消息处理
 *
 *  @param dic 回执消息字典
 *
 *  @return ReceiptMessage
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理回执消息字典，从消息中获取回执消息
 *
 *  @param dic 回执消息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        NSDictionary *tpMessageDic = [dic getDictionaryValueForKey:@"message"];
        self.message = [Message messageWithDic:tpMessageDic];
        self.receiptAction = [dic getIntegerValueForKey:@"receiptAction"];
    }
}

@end

@implementation TextMessage

/**
 *  将TextMessage转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonTextMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":\"%@\"",@"text",self.text];
    [msg appendFormat:@"}"];
    return msg;
}

/**
 *  将字典转TextMessage
 *
 *  @param dic 文本消息字典 -- 这个字典为文本消息JSON字符串转化而来
 *
 *  @return 文本消息
 */
+(TextMessage *)textMessageWithDic:(NSDictionary *)dic
{
    return [[TextMessage alloc]initWithDic:dic];
}

/**
 *  重写init方法，用于文本消息处理
 *
 *  @param dic 文本消息字典
 *
 *  @return TextMessage
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理文本消息字典，从消息中获取文本消息
 *
 *  @param dic 文本消息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        self.text = [dic getStringValueForKey:@"text"];
    }
}

@end

@implementation ImageMessage

/**
 *  将ImageMessage转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonImageMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":\"%@\",",@"path",self.path];
    [msg appendFormat:@"\"%@\":\"%@\",",@"url",self.url];
    [msg appendFormat:@"\"%@\":%f,",@"width",self.width];
    [msg appendFormat:@"\"%@\":%f",@"height",self.height];
    [msg appendFormat:@"}"];
    return msg;
}

/**
 *  将字典转ImageMessage
 *
 *  @param dic 图片信息字典 -- 这个字典为图片信息JSON字符串转化而来
 *
 *  @return 图片信息
 */
+(ImageMessage *)imageMessageWithDic:(NSDictionary *)dic
{
    return [[ImageMessage alloc]initWithDic:dic];
}

/**
 *  重写init方法，用于图片信息处理
 *
 *  @param dic 图片信息字典
 *
 *  @return ImageMessage
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理图片信息字典，从消息中获取图片信息
 *
 *  @param dic 图片信息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        self.path = [dic getStringValueForKey:@"path"];
        self.url = [dic getStringValueForKey:@"url"];
        self.width = [dic getFloatValueForKey:@"width"];
        self.height = [dic getFloatValueForKey:@"height"];
    }
}

@end

@implementation AudioMessage

/**
 *  将AudioMessage转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonAudioMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":\"%@\",",@"path",self.path];
    [msg appendFormat:@"\"%@\":\"%@\",",@"url",self.url];
    [msg appendFormat:@"\"%@\":%ld",@"length",self.length];
    [msg appendFormat:@"}"];
    return msg;
}

/**
 *  将字典转成AudioMessage
 *
 *  @param dic 音频信息字典 -- 这个字典为音频信息JSON字符串转化而来
 *
 *  @return 音频信息
 */
+(AudioMessage *)audioMessageWithDic:(NSDictionary *)dic
{
    return [[AudioMessage alloc]initWithDic:dic];
}

/**
 *  重写init方法，用于音频信息处理
 *
 *  @param dic 音频信息字典
 *
 *  @return AudioMessage
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理音频信息字典，从消息中获取音频信息
 *
 *  @param dic 音频信息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        self.path = [dic getStringValueForKey:@"path"];
        self.url = [dic getStringValueForKey:@"url"];
        self.length = [dic getIntegerValueForKey:@"length"];
    }
}

@end

@implementation LocationMessage

/**
 *  将LocationMessage转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonLocationMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":\"%@\",",@"address",self.address];
    [msg appendFormat:@"\"%@\":\"%@\",",@"city",self.city];
    [msg appendFormat:@"\"%@\":%f,",@"latitude",self.location.latitude];
    [msg appendFormat:@"\"%@\":%f",@"longitude",self.location.longitude];
    [msg appendFormat:@"}"];
    return msg;
}

/**
 *  将字典转成LocationMessage
 *
 *  @param dic 位置信息字典
 *
 *  @return LocationMessage
 */
+(LocationMessage *)locationMessageWithDic:(NSDictionary *)dic
{
    return [[LocationMessage alloc]init];
}

/**
 *  重写init方法，用于位置信息数据处理
 *
 *  @param dic 位置信息字典
 *
 *  @return LocationMessage
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理位置信息字典 -- 获取字典中的数据
 *
 *  @param dic 位置信息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        self.address = [dic getStringValueForKey:@"address"];
        self.city = [dic getStringValueForKey:@"city"];
        Location tpLocation;
        tpLocation.latitude = [dic getFloatValueForKey:@"latitude"];
        tpLocation.longitude = [dic getFloatValueForKey:@"longitude"];
        self.location = tpLocation;
    }
}

@end

@implementation Message

-(id)init
{
    self = [super init];
    if (self)
    {
        NSString *timeString = [NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]];
        self.sendTime = timeString;
        self.type = FriendsMessage;
        self.messageActionType = SendMessage;
        self.sendType = MessageIsSending;
    }
    return self;
}

/**
 *  将消息转成JSON字符串
 *
 *  @return JSON字符串
 */
-(NSString *)jsonMessage
{
    NSMutableString* msg = [NSMutableString stringWithString:@""];
    [msg appendFormat:@"{"];
    [msg appendFormat:@"\"%@\":\"%@\",",@"fromId",self.fromId];
    [msg appendFormat:@"\"%@\":\"%@\",",@"toId",self.toId];
    [msg appendFormat:@"\"%@\":%@,",@"content",[self jsonMessageContent]];
    [msg appendFormat:@"\"%@\":\"%@\",",@"sendTime",self.sendTime];
    [msg appendFormat:@"\"%@\":%ld,",@"type",self.type];
    [msg appendFormat:@"\"%@\":%ld",@"messageType",self.messageType];
    [msg appendFormat:@"}"];
    return msg;
}

-(NSString *)jsonMessageContent
{
    NSString *tpString;
    if (self.messageType == MessageTypeForReceipt)
    {
        //回执消息
        ReceiptMessage *tpReceiptMessage = self.content;
        tpString = [tpReceiptMessage jsonReceiptMessage];
    }
    else if (self.messageType == MessageTypeForText)
    {
        //文本消息
        TextMessage *tpTextMessage = self.content;
        tpString = [tpTextMessage jsonTextMessage];
    }
    else if (self.messageType == MessageTypeForImage)
    {
        //图片消息
        ImageMessage *tpImageMessage = self.content;
        tpString = [tpImageMessage jsonImageMessage];
    }
    else if (self.messageType == MessageTypeForAudio)
    {
        //语音消息
        AudioMessage *tpAudioMessage = self.content;
        tpString = [tpAudioMessage jsonAudioMessage];
    }
    else if (self.messageType == MessageTypeForExp)
    {
        //大表情消息
        
    }
    return tpString;
}

/**
 *  将字典转消息 -- 因为XMPP收到的消息为一个JS字符串，JS字符串需要转字典，而字典需要转Message。
 *
 *  @param dic 消息字典
 *
 *  @return Message
 */
+(Message *)messageWithDic:(NSDictionary *)dic
{
    return [[Message alloc]initWithDic:dic];
}

/**
 *  重写init方法，用于消息数据处理
 *
 *  @param dic 消息字典
 *
 *  @return Message
 */
-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self)
    {
        [self dealDic:dic];
    }
    return self;
}

/**
 *  处理消息字典 -- 获取字典中的数据
 *
 *  @param dic 消息字典
 */
-(void)dealDic:(NSDictionary *)dic
{
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]] && dic.count >0)
    {
        self.fromId = [dic getStringValueForKey:@"fromId"];
        self.toId = [dic getStringValueForKey:@"toId"];
        self.content = [self dealMessageContent:dic];
        self.type = [dic getIntegerValueForKey:@"type"];
        self.messageType = [dic getIntegerValueForKey:@"messageType"];
        self.sendTime = [dic getStringValueForKey:@"sendTime"];
        NSString *timeString = [NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]];
        self.recivedTime = timeString;
        self.messageActionType = ReceivedMessage;
        self.sendType = MessageSendSuccess;
    }
}

/**
 *  从字典中获取消息内容
 *
 *  @param dic 消息字典
 *
 *  @return 返回的消息
 */
-(id)dealMessageContent:(NSDictionary *)dic
{
    id content;
    NSDictionary *tpDic = [dic getDictionaryValueForKey:@"content"];
    MessageType tpMessageType = [dic getIntegerValueForKey:@"messageType"];
    if (tpMessageType == MessageTypeForReceipt)
    {
        //回执消息
        ReceiptMessage *tpReceiptMessage = [ReceiptMessage receiptMessageWithDic:tpDic];
        content = tpReceiptMessage;
    }
    else if (tpMessageType == MessageTypeForText)
    {
        //文本消息
        TextMessage *tpTextMessage = [TextMessage textMessageWithDic:tpDic];
        content = tpTextMessage;
    }
    else if (tpMessageType == MessageTypeForImage)
    {
        //图片消息
        ImageMessage *tpImageMessage = [ImageMessage imageMessageWithDic:tpDic];
        content = tpImageMessage;
    }
    return content;
}

#pragma mark - FMDB

/**
 *  存储消息
 *
 *  @param message 消息
 */
-(BOOL)saveMessage:(Message *)message
{
    return YES;
}

@end
